function loadScript(e,b){var d=document.getElementsByTagName("head")[0];if(d){var c=document.createElement("script");c.setAttribute("src",e);c.setAttribute("type","text/javascript");var a=function(){if(this.readyState=="complete"||this.readyState=="loaded"){b()}};c.onreadystatechange=a;c.onload=b;d.appendChild(c)}};
function get_links(html)
{
    var links = [];
    $(html).find(".main-list .list-title a").each(function(){
        if($(this).find('img').size() > 0)
            return true;
        var text = $(this).text();
        var url = $(this).attr('href');
        links.push(text + "@@@@@" + url);
    });
    return links;
}

function get_items(html)
{
    var l = location,
        db = window.localStorage;
        
        var links = get_links(html);
        // console.log(links);return;
    
        db.links += links.join("\n") + "\n";

        var next_page = $(html).find(".page .next").attr('href');
        // console.log(next_page);return;
        if(next_page)
        {
            console.log(next_page);
            $.get(next_page, get_items);
        }
        else 
        {
            "latest page, create text area";
            console.log('last page');
            var html = "";
            $("body").html("<textarea id='links' style='width:80%;height:400px;padding:10px;float:none;'>" + db.links + "\n" + "</textarea>");
            $("textarea").show();
            $("#links").get(0).select(0);
        
        }
}

function init()
{
   window.localStorage.links = '';

   var url = location.href + '/listall/1';
   $.get(url, get_items);
}

if(typeof jQuery === 'undefined')
  loadScript("https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js", init);
else
{
  var $ = jQuery;
  init();
}


